import test from "@/uni_modules/uview-ui/libs/function/test"
import utils from "@/uni_modules/uview-ui/libs/function/index"
import base from '@/utils/config'
import {
	JumpTypeEnum
} from "@/enums/jump"

export const jump = params => {
	const {
		to = params, mode = 'navigateTo', login = base.LOGIN_CON, open = JumpTypeEnum.self, query
	} = params;
	const type = typeof to;
	const url = ['string'].includes(type) ? _formatUrl(to, query) : ''
	if (open === JumpTypeEnum.blank) {
		// #ifdef H5
		window.open(url)
		// #endif
	} else {
		switch (mode) {
			case 'navigateTo':
				uni.navigateTo({
					url
				});
				break;
			case 'redirectTo':
				uni.redirectTo({
					url
				});
				break;
			case 'reLaunch':
				uni.reLaunch({
					url
				});
				break;
			case 'switchTab':
				uni.switchTab({
					url
				});
				break;
			default:
				uni.navigateTo({
					url
				});
				break;
		}
	}
}

export const _formatUrl = (url, params) => {
	if (!url || !test.object(params) || !params) return url;
	const hasPrefix = url.includes("?")
	const paramStr = utils.queryParams(params, !hasPrefix)
	return url + `${hasPrefix? '&':''}` + paramStr;
}
